#include "h264_set.h"

#include <string.h>
#include "h264_ctx.h"

void init_sps(struct sps_param_t *sps, h264_frame_t *frame)
{
    memset(sps, 0, sizeof(*sps));
    sps->profile_idc = PROFILE_HIGH;

    if (sps->profile_idc == PROFILE_BASELINE) {
        sps->b_constraint_set0_flag = 1;
    } 
    
    if (sps->profile_idc <= PROFILE_MAIN) {
        sps->b_constraint_set1_flag = 1;
    }

    // sps->level_idc = 40;
    sps->level_idc = 11;
    sps->chroma_format_idc = CHROMA_420;
    sps->b_qpprime_y_zero_transform_bypass = 0;
    sps->seq_scaling_matrix_present_flag = 0;
    sps->i_log2_max_frame_num = 4;
    sps->pic_order_cnt_type = 2;
    sps->i_num_ref_frames = 3;
    sps->b_gaps_in_frame_num_value_allowed = 0;
    sps->i_mb_width = frame->mb_width;
    sps->i_mb_height = frame->mb_height;
    sps->b_frame_mbs_only = 1;
    sps->b_mb_adaptive_frame_field = 0;
    sps->b_direct8x8_inference = 1;

    if (frame->mb_width * H264_MB_SIZE > frame->width ||
        frame->mb_height * H264_MB_SIZE > frame->height) {
        sps->b_crop = 1;
        sps->crop_left = 0;
        sps->crop_right = sps->i_mb_width * H264_MB_SIZE - frame->width;
        sps->crop_top = 0;
        sps->crop_bottom = sps->i_mb_height * H264_MB_SIZE - frame->height;
    } else {
        sps->b_crop = 0;
    }

    sps->vui_parameters_present_flag = 0;
    memset(&(sps->vui), 0, sizeof(sps->vui));
    // sps->vui.b_aspect_ratio_info_present = 0;
    // sps->vui.b_overscan_info_present = 0;
    // sps->vui.b_signal_type_present = 0;
    // sps->vui.b_timing_info_present = 1;
}

void init_pps(struct pps_param_t *pps, h264_frame_t *frame)
{
    pps->i_id = 0;
    pps->i_sps_id = 0;
    pps->b_cabac = 1;       // 使用cabac编码
    pps->b_pic_order = 0;
    pps->i_num_slice_groups = 1;
    pps->i_num_ref_idx_l0_default_active = 3;
    pps->i_num_ref_idx_l1_default_active = 1;
    pps->b_weighted_pred = 1;
    pps->b_weighted_bipred = 0;

    pps->i_pic_init_qp = 23;        // qp值
    pps->i_pic_init_qs = 26;        // qs值

    pps->i_chroma_qp_index_offset = -2;
    pps->b_deblocking_filter_control = 1;
    pps->b_constrained_intra_pred = 0;
    pps->b_redundant_pic_cnt = 0;
    pps->b_transform_8x8_mode = 1;
}
